/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.swing.calc;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Administrator
 */
public class CalcJDialog extends JDialog{
   private ActionListener actionListener;
   CalcJDialog stant;
   protected CalcJDialog(Frame parent, boolean modal) {
        super(parent, modal);  
    }

   protected CalcJDialog(java.awt.Dialog parent, boolean modal) {
       super(parent, modal);
    }

    @Override
    protected JRootPane createRootPane() {
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane rootPane1 = new JRootPane();
        actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                escapeKeyProc();
            }
        };
        rootPane1.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        CalcKeyBoardPanel calc=new CalcKeyBoardPanel();
        rootPane1.getContentPane().add(calc, java.awt.BorderLayout.CENTER);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-665)/2, (screenSize.height-565)/2, 450, 350);
        return rootPane1;
    }  


     public static void main(String args[]) {
                new CalcJDialog(new Frame(),false).setVisible(true);
     }

     public static void showCalcJDialog(Component parent, boolean modal){
        Window window = getWindow(parent);
        CalcJDialog myMsg;
        if (window instanceof Frame) {
            myMsg = new CalcJDialog((Frame) window, true);
        } else {
            myMsg = new CalcJDialog((Dialog) window, true);
        }
        myMsg.applyComponentOrientation(parent.getComponentOrientation());
        myMsg.setVisible(true);
     }


     private static Window getWindow(Component parent) {
        if (parent == null) {
            return new JFrame();
        } else if (parent instanceof Frame || parent instanceof Dialog) {
            return (Window) parent;
        } else {
            return getWindow(parent.getParent());
        }
    }

    protected  void escapeKeyProc(){
         dispose();
    }
}
